<template>
    <div class="work">
        <!-- 中间弹出框新成员添加 -->

    <!-- 搜索框 -->
    <div class="search">
          <div class="title">
            <span>Staff Management</span>
          </div>
          <div style="width:30%">
            <el-input v-model="searchName" placeholder="please enter">
              <el-button @click="searchOwner()" slot="append">search</el-button>
              <el-button @click="clearSearch()" slot="append">cancel search</el-button>
            </el-input>
          </div>
    </div>

    <!-- 主体内容 -->
    <template>
    <el-table
        :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
        :data="tableData"
        border
        style="width: 100%"
        ref="tableData"
        class="emp_table"
        v-if="tableData">
        <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
        <el-table-column
        align="center"
        prop="pe_number"
        label="PE Number"
        width="180">
        </el-table-column>
        <el-table-column
        align="center"
        prop="pe_name"
        label="Name">
        </el-table-column>
        <el-table-column
        align="center"
        prop="dept_name"
        label="Dept">
          <template slot-scope="{ row }">
            {{ row.dept_name || 'none' }}
          </template>
        </el-table-column>
        <el-table-column
        align="center"
        prop="position"
        label="Position">
        </el-table-column>
        <el-table-column
              align="center"
              label="Action">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="carseList(scope)">Update</el-button>
                </template>
            </el-table-column>
    </el-table>

    </template>
      <el-pagination
        align="right"
        @current-change="curChange"
        :current-page.sync="curPage"
        :page-size="10"
        layout="total,prev, pager, next, jumper"
        :total="count">
      </el-pagination>

      <seeForm ref="seeForm"></seeForm>

    </div>


</template>

<script>
import {manageIndex,userSearch} from '../../request/api'
import seeForm from "./see";

export default {
  components:{
    seeForm
  },
    data() {
      return {
        // 搜索内容
        searchName:'',
        // 页面数据
        tableData: [],
        // count:总数据量
        count:0,
        // 当前页
        curPage:1,
        // 弹框是否可见
        dialogVisible: false,
        // 等级
        level:'',
        //当前是否在搜索状态
        isSearch:false
      }
    },
  mounted () {
    this.level = sessionStorage.getItem('level')
  },
  created() {
    manageIndex().then(res => {
      if(res.code === 200){
          this.count = res.count
          this.tableData = res.data
      }
    })
  },
  methods: {
    // 搜索
    searchOwner(){
      userSearch({
        keyword:this.searchName,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
      this.curPage = 1
    },
    //取消搜索
    clearSearch(){
      manageIndex().then(res => {
        if(res.code === 200){
          this.count = res.count
          this.tableData = res.data
          this.searchName = ''
        }
      })
      this.curPage = 1
    },
    //获取全部员工分页
    getAll(){
      manageIndex({
        page: this.curPage,
      }).then(res => {
        if(res.code === 200){
          this.count = res.count
          this.tableData = res.data
        }
      })
    },
    //获取搜索分页
    getSearch(){
      userSearch({
        keyword:this.searchName,
        page:this.curPage,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
    },
    //  点击每页刷新获取数据
    curChange (currentPage) {
      this.curPage = currentPage
      if(this.isSearch) {
        this.getSearch()
      } else {
        this.getAll()
      }
    },
    // 上一页
    prevClick () {
      this.curPage < 1 ? this.curPage = 1 : this.curPage--
      if(this.isSearch) {
        this.getSearch()
      } else {
        this.getAll()
      }
    },
    // 下一页
    nextClick () {
      this.curPage >= this.count ? this.curPage = this.count : this.curPage++
      if(this.isSearch) {
        this.getSearch()
      } else {
        this.getAll()
      }
    },
    carseList(scope){
      this.$refs.seeForm.formatData(scope.row)
      this.$refs.seeForm.dialogVisible = true
      this.$refs.seeForm.isAdd = scope.row.a
    }
  },
}
</script>

<style lang="less">
    .search {
      display: flex;
      justify-content: space-between;
      padding-bottom: 20px;
    }
    .permitImg{
        width: 35px;
        height: 35px;
    }
    .dig-form{
        width: 90%;
        height: 100%;
        margin: 0 auto;
    }
    .preImg{
        width:56px;
        border-radius:4px;
        height:40px;
        margin-right:15px
    }
    .title{
      font-family: SourceHanSansSC;
      font-weight: 700;
      font-size: 20px;
      color: rgb(16, 16, 16);
      font-style: normal;
      letter-spacing: 0px;
      line-height: 30px;
      text-decoration: none;
      margin-bottom: 10px;
    }
</style>


